using System;
using System.Collections.Generic;
using System.Text;
using eYap.src.Forms;
using agsXMPP;
using System.Drawing;
using System.Windows.Forms;
using agsXMPP.protocol.component;
using agsXMPP.protocol.iq.vcard;
using agsXMPP.protocol.client;

namespace eYap
{
    public class Contact
    {
        public Jid jid;
        public string Name;
        public Image photo;
        public int availability;
        public MainForm mainForm;
        public TabPage chatTab;
        public ChatControl chatControl;
        public ContactItem contactListitem;

        public Contact(MainForm mainForm, Jid jid)
        {
            this.mainForm = mainForm;
            this.jid = jid;
            this.Name = "";
            this.photo = null; // TODO: set initial default avatar
        }

        public void GetUIReferences(TabPage tab, ChatControl chtControl)
        {
            this.chatTab = tab;
            this.chatControl = chtControl;
        }

        public void CreateListViewItem()
        {
            if (mainForm.InvokeRequired)
            {
                MethodInvoker invoker = new MethodInvoker(this.CreateListViewItem);
                mainForm.BeginInvoke(invoker, null);
            }
            else
            {
                if (this.photo != null)
                {
                    mainForm.imageList.Images.Add(this.photo);
                }
                else
                {
                    mainForm.imageList.Images.Add(eYap.Properties.Resources.profile_image);
                }
                contactListitem = new ContactItem(); 
                contactListitem.AssignContact(this, mainForm.imageList.Images.Count - 1);
                contactListitem.SetStatusTextColor(this.availability);
                mainForm.lstView_ContactList.Items.Add(contactListitem);
            }
        }

        public void RemoveListViewItem()
        {
            if (mainForm.InvokeRequired)
            {
                MethodInvoker invoker = new MethodInvoker(this.RemoveListViewItem);
                mainForm.BeginInvoke(invoker, null);
            }
            else
            {
                mainForm.lstView_ContactList.Items.Remove(this.contactListitem);
            }
        }

        public void UpdateContactAvailability(int availability)
        {
            if (mainForm.InvokeRequired)
            {
                mainForm.BeginInvoke(new MethodInvoker(delegate(){this.UpdateContactAvailability(availability);}));
            }
            else
            {
                this.availability = availability;

                if (this.contactListitem != null)
                {
                    this.contactListitem.SetStatusTextColor(availability);
                }
                if (this.chatControl != null)
                {
                    this.chatControl.SetContactAvailability(availability);
                }
            }
        }

        public void UpdateContactChatStatus(int status)
        {
            if (mainForm.InvokeRequired)
            {
                mainForm.BeginInvoke(new MethodInvoker(delegate() { this.UpdateContactChatStatus(status); }));
            }
            else
            {
                if (this.chatControl != null)
                {
                    this.chatControl.SetChatStatus(status);
                }
            }
        }

        public void GetMessage(string msg)
        {
            if (mainForm.InvokeRequired)
            {
                mainForm.BeginInvoke(new MethodInvoker(delegate() { this.GetMessage(msg); }));
            }
            else
            {
                chatControl.PrintRecievedMessage(msg);
            }
        }

        public void GetVCard(XmppClientConnection xmppClient)
        {
            VcardIq viq = new VcardIq(IqType.get, this.jid);
            xmppClient.IqGrabber.SendIq(viq, new IqCB(GetVCardResultCallBack), null);
        }

        private void GetVCardResultCallBack(object sender, agsXMPP.protocol.client.IQ iq, object data)
        {
            if (iq.Vcard != null)
            {
                this.Name = iq.Vcard.Fullname;
                Photo tphoto = iq.Vcard.Photo;
                if (tphoto != null)
                {
                    this.photo = tphoto.Image;
                }
            }
            this.CreateListViewItem();
        }
    }
}
